package cn.iocoder.yudao.module.blog.service.poster;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.poster.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.poster.PosterConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.poster.PosterMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 海报 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class PosterServiceImpl implements PosterService {

    @Resource
    private PosterMapper posterMapper;

    @Override
    public Long createPoster(PosterCreateReqVO createReqVO) {
        // 插入
        PosterDO poster = PosterConvert.INSTANCE.convert(createReqVO);
        posterMapper.insert(poster);
        // 返回
        return poster.getId();
    }

    @Override
    public void updatePoster(PosterUpdateReqVO updateReqVO) {
        // 校验存在
        validatePosterExists(updateReqVO.getId());
        // 更新
        PosterDO updateObj = PosterConvert.INSTANCE.convert(updateReqVO);
        posterMapper.updateById(updateObj);
    }

    @Override
    public void deletePoster(Long id) {
        // 校验存在
        validatePosterExists(id);
        // 删除
        posterMapper.deleteById(id);
    }

    private void validatePosterExists(Long id) {
        if (posterMapper.selectById(id) == null) {
            throw exception(POSTER_NOT_EXISTS);
        }
    }

    @Override
    public PosterDO getPoster(Long id) {
        return posterMapper.selectById(id);
    }

    @Override
    public List<PosterDO> getPosterList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return posterMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PosterDO> getPosterPage(PosterPageReqVO pageReqVO) {
        return posterMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PosterDO> getPosterList(PosterExportReqVO exportReqVO) {
        return posterMapper.selectList(exportReqVO);
    }

}
